<?php

function BBCode($string,$override="",$highlight=""){
	global $configarray,$indpostarray,$languagedef;
	unset($trans);
	
	if($indpostarray[10]!=="on"||$override=="1"){
		$bbcodearray=bbcodearray();
		
		for($n=0;$n<count($bbcodearray);$n++){
			$linearray=explode("\t",$bbcodearray[$n]);
			
			if($linearray[2]=="on"){
				$trans['{param}']="\\1";
				$trans['{option}']="\\2";
				$linearray[3]=strtr($linearray[3],$trans);
				for($m=0;$m<$configarray[43];$m++){
					$replacearray[]=$linearray[3];
					$patternarray[]="/\[".$linearray[1]."=(.*?)\](.*?)\[\/".$linearray[1]."\]/i";
				}
			}else{
				$trans['{option}']="\\1";
				$linearray[3]=strtr($linearray[3],$trans);
				for($m=0;$m<$configarray[43];$m++){
					$replacearray[]=$linearray[3];
					$patternarray[]="/\[".$linearray[1]."\](.*?)\[\/".$linearray[1]."\]/i";
				}
			}
			
		}//end bbcode loop
		
		//quotes
		for($m=0;$m<$configarray[43];$m++){
			$replacearray[]=template('quote',array('user'=>"\\1",'quote'=>"\\2"));
		
			$patternarray[]="/\[quote=(.*?)\](.*?)\[\/quote\]/i";
		}
		
	}//disable check
	if($highlight){
		unset($trans);
		$highlight=split(" ",$highlight);
		for($n=0;$n<count($highlight);$n++){
			unset($tplcontent);
			$tplcontent['highlight']=$highlight[$n];
			$trans[$highlight[$n]]=template('highlight',$tplcontent);
		}
		$string=strtr($string,$trans);
	}
	
	//line breaks cannot be disabled\
	
	$string=preg_replace($patternarray,$replacearray,$string,$configarray[4]);
	
	//this is seperate because breaks cant be limited
	unset($patternarray);
	unset($replacearray);
	
	$patternarray[]="/\[br\]/i";
	$replacearray[]="<br>";
	
	$string=preg_replace($patternarray,$replacearray,$string);
	
	return $string;
}

function smilies($string,$override=""){
	global $configarray,$indpostarray,$globalsmiliesarray,$globalbbcodearray;
	unset($trans);
	
	if($indpostarray[12]!=="on"||$override=="1"){
		$smiliesarray=smiliesarray();
		
		unset($replacearray);
		unset($patternarray);
		for($n=0;$n<count($smiliesarray);$n++){
			
			$linearray=explode("\t",$smiliesarray[$n]);
			
			//these have to be escaped to be used with preg_replace, smilies may or may not use these
			$linearray[0]=str_replace(")","\)",$linearray[0]);
			$linearray[0]=str_replace("(","\(",$linearray[0]);
			$linearray[0]=str_replace("|","\|",$linearray[0]);
			
			$patternarray[$n]="/".$linearray[0]."/i";
			$replacearray[$n]="<img src=\"images/smilies/$linearray[1]\">";
			
		}//end smilies loop
		
		$string=preg_replace($patternarray,$replacearray,$string,$configarray[3]);
	}
	
	return $string;
}

function subforums($parentforum,$level){
	global $configarray,$forumlist;
	
	if($forumlist[$parentforum]){$forumsublist=explode(",",$forumlist[$parentforum]);}
	for($n=0;$n<count($forumsublist);$n++){
		$forumconfigarray=getdata("$configarray[2]/$forumsublist[$n].php");
		if(getforumaccess("show",$forumsublist[$n])&&$level<$configarray[27]&&$level<30){
			displayforumrow($forumsublist[$n],$level+1,$forumconfigarray);
			subforums($forumsublist[$n],$level+1);
		}
	}
	
}//end sub forums function


#####  DISPLAY FORUM ROW FUNCTION  #####
function displayforumrow($forum,$level,$forumconfigarray){
	global $configarray,$theme,$userloggedinarray,$mainfile,$useridarray,$userkeyarray,$dateformat,$login,$allforumconfigarray,$alluserarray,$alltopicarray,$allpostarray,$theme_imagespath,$tplcontent,$timeoffsetseconds,$languagedef;
	
	$topicarray=listdirs("$configarray[2]/$forum");
	
	if($forumconfigarray[5]=="category"){
		unset($tplcontent2);
		
		$width=($level*$configarray[44])+2;
		$tplcontent2['spacing'].="$width%";
		
		$width=99-($level*$configarray[44]);
		
		unset($tplcontent3);
		$tplcontent3['width'].="$width";
		$tplcontent3['name'].="<a href=\"index.php?forum=$forum\">$forumconfigarray[3]</a>";
		$tplcontent3['description'].=$forumconfigarray[2]."&nbsp;";
		$tplcontent2['categoryname'].=template('forumcategoryname',$tplcontent3);
		
		$tplcontent['forumrows'].=template('forumrowcategory',$tplcontent2);
		
	}else{
		unset($tplcontent2);
		
		if(count($topicarray)>0){
			$topictime=newestthread($forum);
			$topicnum=topic_timetonum($forum,$topictime);
			
			$postarray=listfiles("$configarray[2]/$forum/$topictime");
			if(count($postarray)>0){
				@rsort($postarray,SORT_NUMERIC);
				$lastpostarray=getdata("$configarray[2]/$forum/$topictime/$postarray[0].php");
				@sort($postarray,SORT_NUMERIC);
			}
			
		}
		
		$lastposton=$lastpostarray[1];
		
		if(getforumaccess("view",$forum,"")){
			$lastloginarray=explode("\t",$userloggedinarray[5]);
			if($lastposton>$lastloginarray[0]){$tplcontent2['icon'].="<img src=\"$theme_imagespath/new.gif\" alt=\"\">";}
			if($lastposton<=$lastloginarray[0]){$tplcontent2['icon'].="<img src=\"$theme_imagespath/nonew.gif\" alt=\"\">";}
		}else{
			$tplcontent2['icon'].="<img src=\"$theme_imagespath/locked.gif\" alt=\"\">";
		}
		
		$width=($level*$configarray[44])+1;
		//spacing before forum name
		$tplcontent2['spacing'].="$width%";
		
		$width=99-($level*$configarray[44]);
		//forum name/description
		$tplcontent3['width'].=$width;
		$tplcontent3['name'].="<a href=\"index.php?forum=$forum\">$forumconfigarray[3]</a>";
		$tplcontent3['description'].=$forumconfigarray[2];
		$tplcontent2['forumname'].=template('forumname',$tplcontent3);
		
		if(count($topicarray)>0){
			$lastpostby=$lastpostarray[0];
			$lastposton=$lastpostarray[1];
			$lastposton=date($dateformat,$lastposton+$timeoffsetseconds);
			
			$tplcontent2['lastpost'].="$lastposton<br>";
			
			$userarray=getdata($configarray[1]."/$lastpostby/main.php");
			
			if(count($userarray)>0){
				$tplcontent2['lastpost'].=$languagedef['by']." <b><a href=\"profile.php?user=$lastpostby\">$userarray[0]</a></b>";
			}else{
				$tplcontent2['lastpost'].=$languagedef['by']." <b>Guest</a></b>";
			}
			
		}else{
			$tplcontent2['lastpost'].=$languagedef['never'];
		}
		
		if(count($topicarray)>0){
			@rsort($postarray,SORT_NUMERIC);
			$tplcontent2['gotoicon'].="&nbsp;<a href=\"index.php?forum=$forum&topic=$topicnum&page=last#post".($postarray[0]+1)."\"><img border=\"0\" src=\"$theme_imagespath/goto.gif\"></a>";
			@sort($postarray,SORT_NUMERIC);
		}else{
			$tplcontent2['gotoicon'].="&nbsp;";
		}
		
		$tplcontent2[topics].=count($topicarray);
		
		if($configarray[42]=="on"){
			$replies=$forumconfigarray[11];
		}else{
			$replies=0;
			for($m=0;$m<count($topicarray);$m++){
				$postarray2=listfiles("$configarray[2]/$forum/$topicarray[$m]");
				$replies+=count($postarray2)-1;
			}
		}
		$tplcontent2['replies'].="$replies";
		
		//show moderators
		$moderatorsarray=explode(",",$forumconfigarray[1]);
		if($moderatorsarray[0]!==""){
			for($m=0;$m<count($moderatorsarray);$m++){
				$userarray=getdata("$configarray[1]/$moderatorsarray[$m]/main.php");
				
				if(count($userarray)>0){
					$tplcontent2['moderators'].="<a href=\"profile.php?user=$moderatorsarray[$m]\">$userarray[0]</a>";
				}else{
					$tplcontent2['moderators'].=$languagedef[226]." ($moderatorsarray[$m])";
				}
				
				if($m<count($moderatorsarray)-1){$tplcontent2['moderators'].=", ";}
			}
		}else{
			$tplcontent2['moderators'].="&nbsp;";
		}
		
		$tplcontent[forumrows].=template('forumrow',$tplcontent2);
	}//category bracket
	
}//end display forum row function

function forumsmenu($parentforum,$level,$selected=""){
	global $configarray,$forumlist,$allforumconfigarray;
	unset($content);
	
	if($forumlist[$parentforum]){$forumsublist=explode(",",$forumlist[$parentforum]);}
	for($m=0;$m<count($forumsublist);$m++){
		$forumconfigarray=getdata("$configarray[2]/$forumsublist[$m].php");
		if(getforumaccess("show",$forumsublist[$m])&&$level<$configarray[27]){
			if($forumsublist[$m]==$selected){
				$content.="<option value=\"$forumsublist[$m]\" selected>";
			}else{
				$content.="<option value=\"$forumsublist[$m]\">";
			}//selected test
			
			for($l=0;$l<$level;$l++){
				$content.=">";
			}
			
			$content.=" $forumconfigarray[3]</option>\n";
			$content.=forumsmenu($forumsublist[$m],$level+1,$selected);
		}//access/level test
	}//loop
	
	return $content;
}//end forums menu function

function addattachment(){
	global $configarray,$forum,$thread,$post,$attachmentfile,$attachmentfile_name,$fileext,$attachstatus;

	unset($attachstatus);
	$attachnamearray=explode(".",$attachmentfile_name);
	$end=count($attachnamearray)-1;
	$extarray=explode(",",$configarray[22]);
	
	$allattachments=listfilesext("attachments");
	$totalsize=0;
	for($n=0;$n<count($allattachments);$n++){
		$totalsize+=filesize("attachments/$allattachments[$n]");
	}
	
	//exist check
	if(!@in_array($attachmentfile_name,$allattachments)){
		
		//total space check
		if(($totalsize+filesize($attachmentfile))<$configarray[31]){
			
			//type check			
			if(@in_array($attachnamearray[$end],$extarray)){
				
				//file size check
				if(filesize($attachmentfile)<$configarray[23]){
					
					if(@move_uploaded_file($attachmentfile,"attachments/$attachmentfile_name")){
						@chmod("attachments/$attachmentfile_name",octdec(777));
						$attachstatus=1;
						return template('noticeattachsuccess');
					}else{
						$attachstatus=0;
						return template('noticeattachfailed');
					}
					
				}else{
					$attachstatus=0;
					return template('noticeattachfilesize',array('size'=>$configarray[23]));
				}
			}else{
				$attachstatus=0;
				return template('noticeattachtype',array('ext'=>$configarray[22]));
			}
		}else{
			$attachstatus=0;
			return template('noticeattachspacelimit');
		}
	}else{
		$attachstatus=0;
		return template('noticeattachnameexist');
	}
	
}

function topiclistbuttons(){
	global $configarray,$userloggedinarray,$allforumconfigarray,$alluserarray,$forum,$theme,$alltemplates,$replacements,$theme_templates,$theme_replacements,$theme_imagespath;
	
	unset($tplcontent);
	$tplcontent['newtopic'].="<a href=\"post.php?forum=$forum\"><img src=\"$theme_imagespath/newtopic.gif\" border=0></a>";
	$content=template('topiclistbuttons',$tplcontent);
	
	return $content;
}

function topicbuttons(){
	global $configarray,$userloggedinarray,$allforumconfigarray,$alluserarray,$forum,$topicnum,$page,$theme,$alltemplates,$replacements,$theme_templates,$theme_replacements,$theme_imagespath;
	
	unset($tplcontent);
	$tplcontent['newtopic'].="<a href=\"post.php?forum=$forum\"><img src=\"$theme_imagespath/newtopic.gif\" border=0></a>";
	$tplcontent['reply'].="<a href=\"post.php?forum=$forum&topic=$topicnum&page=$page\"><img src=\"$theme_imagespath/reply.gif\" border=0></a>";
	$content=template('postlistbuttons',$tplcontent);
	
	return $content;
}

function getforumaccess($type,$forum="",$user=""){
	global $configarray,$userloggedinarray,$allforumconfigarray,$alluserarray;
	
	if($forum==""){global $forumconfigarray;}
	if($forum!==""){$forumconfigarray=getdata("$configarray[1]/$forum.php");}
	if($user!==""){$userarray=alluserarray($user);}else{$userarray=$userloggedinarray;}
	
	$access=1;
	
	if($type=="view"){
		$forumgroupsarray=explode(",",$forumconfigarray[0]);
		if(@in_array($userarray[15],$forumgroupsarray)){$access=0;}
	}
	
	if($type=="topic"){
		$forumgroupsarray=explode(",",$forumconfigarray[7]);
		if(@in_array($userarray[15],$forumgroupsarray)||$forumconfigarray[5]=="category"){$access=0;}
	}
	
	if($type=="reply"){
		$forumgroupsarray=explode(",",$forumconfigarray[8]);
		if(@in_array($userarray[15],$forumgroupsarray)){$access=0;}
	}
	
	if($type=="edit"){
		$forumgroupsarray=explode(",",$forumconfigarray[9]);
		if(@in_array($userarray[15],$forumgroupsarray)){$access=0;}
	}
	
	if($type=="show"){
		$forumgroupsarray=explode(",",$forumconfigarray[10]);
		if(@in_array($userarray[15],$forumgroupsarray)){$access=0;}
	}
	
	return $access;
}

function geteditaccess(){
	global $configarray,$useridarray,$navboardlogin,$indpostarray,$userloggedinarray,$moderatorsarray,$login;
	
	$access=0;
	$indpostarray[0]=(float) $indpostarray[0];
	$useridarray[$navboardlogin]=(float) $useridarray[$navboardlogin];
	if(($useridarray[$navboardlogin]==$indpostarray[0]||$userloggedinarray[15]=="administrator"||@in_array("$useridarray[$navboardlogin]",$moderatorsarray))&&$login==1)
	{
		$access=1;
	}
	return $access;
}

function loginaccess($cookieset){
	global $banarray,$useridarray,$navboardlogin,$navboardpass,$rememberme,$configarray,$QUERY_STRING,$userloggedinarray;
	
	$navboardlogin==substr($navboardlogin,0,30);
	$navboardlogin==stripslashes($navboardlogin);
	$navboardlogin==htmlentities($navboardlogin);
	
	$navboardpass==stripslashes($navboardpass);
	$navboardpass==htmlentities($navboardpass);
	
	$userloggedinarray=getdata("$configarray[1]/".$useridarray[$navboardlogin]."/main.php");
	
	if(count($userloggedinarray)>0)
	{
		
		if(md5($navboardpass)==$userloggedinarray[1])
		{
			
			if($userloggedinarray[15]!=="confirm"&&$userloggedinarray[15]!=="approve")
			{
				
				$emailarray=explode("\t",$userloggedinarray[2]);
				
				if(!checkban($useridarray[$navboardlogin],$userloggedinarray[19],$emailarray[0],$userloggedinarray[15]))
				{
					$login=1;
					if($cookieset){
						if($rememberme=="on"){
							@setcookie("navboardlogin","$navboardlogin",time()+100000000);
							@setcookie("navboardpass","$navboardpass",time()+100000000);
						}else{
							@setcookie("navboardlogin","$navboardlogin");
							@setcookie("navboardpass","$navboardpass");
						}
					}
				}else{
					$login=3;
				}//ban check
				
			}else{
				$login=5;
			}//confirm check
			
		}else{
			$login=4;
		}//pass check
		
	}else{
		$login=2;
	}//exist check
	
	return $login;
}

function bodyparse($body){
	global $configarray,$indpostarray,$highlight;
	
	if($configarray[14]!=="allowhtml"){
		$body=htmlentities($body);
	}
	
	$body=smilies($body);
	$body=BBCode($body,"",$highlight);
	
	$body=ereg_replace("{br}","&#091;br&#093;",$body);
	
	return $body;
}

function displaysmilies(){
	global $configarray,$globalsmiliesarray,$globalbbcodearray;
	
	$smiliesarray=smiliesarray();
	
	for($n=0;$n<count($smiliesarray);$n++){
		for($m=0;($m<$configarray[47])&&($n<count($smiliesarray));$m++){
			$linearray=explode("\t",$smiliesarray[$n]);
			$content.="<a href=\"javascript:codeinsert('$linearray[0]')\"><img border=0 src=\"images/smilies/$linearray[1]\"></a> ";
			$n++;
		}
		$content.="<br>";
	}
	
	return $content;
}

function checkpmspace($user){
	global $configarray;
	
	$pmlistarray=listfiles("$configarray[1]/$user/pms");
	@rsort($pmlistarray,SORT_NUMERIC);
	
	$total['size']=0;
	$total['number']=count($pmlistarray);
	$total['percentsize']=0;
	$total['percentnumber']=0;
	
	for($n=0;$n<count($pmlistarray);$n++){
		$total['size']+=filesize("$configarray[1]/$user/pms/$pmlistarray[$n].php");
	}
	
	if($total['size']>0){
		$total['percentsize']=round((($total['size']/$configarray[29])*100),1);
	}
	if($total['number']>0){
		$total['percentnumber']=round((($total['number']/$configarray[30])*100),1);
	}
	
	return $total;
}

function checkdupdisplay($displayname){
	global $usersarray,$user,$configarray,$mainfile,$alluserarray;
	
	$dup=0;
	if($configarray[32]!=="on"){
		
		for($n=0;$n<count($usersarray);$n++){
			$userarray=getdata("$configarray[1]/$usersarray[$n]/main.php");
			if($displayname==$userarray[0]&&"$usersarray[$n]"!=="$user"){$dup=1;break 1;}
		}
		
	}
	
	return $dup;
}

function usersmenu($selected){
	global $usersarray,$configarray,$mainfile,$alluserarray;
	
	@sort($usersarray);
	$content.="<option value=\"\"></option>";
	for($n=0;$n<count($usersarray);$n++){
		$userarray=getdata("$configarray[1]/$usersarray[$n]/main.php");
		if("$selected"=="$usersarray[$n]"){
			$content.="<option value=\"$usersarray[$n]\" selected>$userarray[0]</option>";
		}else{
			$content.="<option value=\"$usersarray[$n]\">$userarray[0]</option>";
		}
	}
	
	return $content;
}

function mail2($contact,$subject,$message){
	global $configarray,$alluserarray;
	
	//if user 'contact' exists use their email otherwise use send it to 'contact'
	$userarray=getdata("$configarray[1]/$contact/main.php");
	if(count($userarray)>0){
		$emailarray=explode("\t",$userarray[2]);
		$email=$emailarray[0];
	}else{
		$email=$contact;
	}
	
	$headers = "MIME-Version: 1.0\r\n";
	$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
	$headers .= "From: $configarray[0]\r\n";
	$headers .= "To: $userarray[0]\r\n";
	
	mail($email,$subject,$message,$headers);
	
}

function deleteuser($user){
	global $configarray;
	$pmarray=listfiles("$configarray[1]/$user/pms");
	for($n=0;$n<count($pmarray);$n++){
		@unlink("$configarray[1]/$user/pms/$pmarray[$n].php");
	}
	@rmdir("$configarray[1]/$user/pms");
	$filesarray=listfilesext("$configarray[1]/$user");
	for($n=0;$n<count($filesarray);$n++){
		@unlink("$configarray[1]/$user/$filesarray[$n]");
	}
	@rmdir("$configarray[1]/$user");
	writedata("$configarray[1]/accounts.php","",$user);
}

function deletetopic($forum,$topictime){
	global $configarray,$allforumconfigarray,$allpostarray,$alluserarray;
	
	$postarray=listfiles("$configarray[2]/$forum/$topictime");
	if($configarray[42]=="on"){
		$forumconfigarray=getdata("$configarray[2]/$forum.php");
		$forumconfigarray[11]-=(count($postarray)-1);
		writedata("$configarray[2]/$forum.php",$forumconfigarray[11],11);
	}
	
	for($n=0;$n<count($postarray);$n++){
		$indpostarray=getdata("$configarray[2]/$forum/$topictime/$postarray[$n].php");
		
		$userarray=getdata("$configarray[2]/$indpostarray[0]/main.php");
		if(count($userarray)>0){
			$posts=$userarray[6]-1;
			writedata("$configarray[1]/$indpostarray[0]/main.php",$posts,6);
		}
		
		if($indpostarray[4]=="attachment"){
			@unlink("attachments/$indpostarray[5]");
		}
		
		@unlink("$configarray[2]/$forum/$topictime/$postarray[$n].php");
	}
	
	
	
	@unlink("$configarray[2]/$forum/$topictime.php");
	
	@rmdir("$configarray[2]/$forum/$topictime");
	
}

function deletepost($forum,$topictime,$post){
	global $configarray,$allpostarray,$allforumconfigarray,$alluserarray;
	$topicnum=topic_timetonum($forum,$topictime);
	$postarray=listfiles("$configarray[2]/$forum/$topictime");
	if($configarray[42]=="on"){
		$forumconfigarray=getdata("$configarray[2]/$forum.php");
		writedata("$configarray[2]/$forum.php",$forumconfigarray[11]-1,11);
	}
	
	$indpostarray=getdata("$configarray[2]/$forum/$topictime/$post.php");
	$userarray=getdata("$configarray[1]/$indpostarray[0]/main.php");
	
	if(count($userarray)>0){
		$posts=$userarray[6]-1;
		writedata("$configarray[1]/$indpostarray[0]/main.php",$posts,6);
	}
	
	unlink("$configarray[2]/$forum/$topictime/$post.php");
	
	@rsort($postarray,SORT_NUMERIC);
	if($post==$postarray[0]){
		$indpostarray2=getdata("$configarray[2]/$forum/$topictime/$postarray[1].php");
		@rename("$configarray[2]/$forum/$topictime","$configarray[2]/$forum/$indpostarray2[1]");
		@rename("$configarray[2]/$forum/$topictime.php","$configarray[2]/$forum/$indpostarray2[1].php");
		writedata("$configarray[2]/${forum}_topics.php",$indpostarray2[1],$topicnum);
	}
	@sort($postarray,SORT_NUMERIC);
}

function newestthread($forum){
	global $configarray,$alltopicarray;
	
	$threadarray=listdirs("$configarray[2]/$forum");
	@rsort($threadarray);
	$newestimp=$threadarray[0];
	$newestimptime=substr($threadarray[0],0,10);
	if($newestimptime=="important_"){$newestimptime=substr($threadarray[0],10);}
	
	for($l=0;$l<count($threadarray)&&substr($threadarray[$l],0,10)=="important_";$l++){}
	$newest=$threadarray[$l];
	$newesttime=$threadarray[$l];
	
	if($newestimptime>$newesttime){$thread=$newestimp;}else{$thread=$newest;}
	
	return $thread;
}

function checkban($username,$ip,$email,$group){
	global $banarray;
	$ban=0;
	
	if((trim($group)&&$group=="banned")||(trim($username)&&@in_array($username,$banarray))||(trim($ip)&&@in_array($ip,$banarray))||(trim($email)&&@in_array($email,$banarray)))
	{$ban=1;}
	
	return $ban;
}

function topic_numtotime($forum,$topicnum){
	global $configarray;
	
	$topicidarray=getdata("$configarray[2]/${forum}_topics.php");
	$topictime=$topicidarray[$topicnum];
	if($topictime){
		return $topictime;
	}else{
		return "0";
	}
}

function topic_timetonum($forum,$topictime){
	global $configarray;
	
	$topicconfigarray=getdata("$configarray[2]/$forum/$topictime.php");
	$topicnum=$topicconfigarray[3];
	return $topicnum;
}

######################
## DISPLAY POST ROW ##
######################
function displaypostrow($forum,$topictime,$post){
	global $postarray,$configarray,$dateformat,$userloggedinarray,$theme,$topicnum,$userlevelsarray,$moderatorsarray,$topicarray,$topicconfigarray,$forumconfigarray,$firstpostarray,$showvoters,$vote,$useridarray,$login,$navboardlogin,$indpostarray,$highlight,$alluserarray,$globalsmiliesarray,$globalbbcodearray,$theme_imagespath,$timeoffsetseconds,$languagedef;
	$indpostarray=getdata("$configarray[2]/$forum/$topictime/$post.php");
	
	
	$userarray=getdata("$configarray[1]/$indpostarray[0]/main.php");
	
	unset($tplcontent2);
	if(count($userarray)>0){
		$tplcontent2['name'].=$userarray[0];
		$tplcontent2['title'].=$userarray[16];
		
		if($userarray[7]) {
			if(eregi("http://(.+)",$userarray[7])){
				$tplcontent2['avatar'].="<img src=\"$userarray[7]\">";
			}else{
				$tplcontent2['avatar'].="<img src=\"avatars/$userarray[7]\">";
			}
			
		}else{
			$tplcontent2['avatar'].="<br><br><br><br>";
		}
		$tplcontent2['group'].=ucfirst($userarray[15]);
		
		if(count($userlevelsarray)>0){
			for($p=1;$p<count($userlevelsarray);$p+=2){
				if($userlevelsarray[$p]>$userarray[6]){break 1;}
			}
			
			if($configarray[45]=="on"){
				$tplcontent2['level'].=$languagedef['level'].": ".$userlevelsarray[$p-3];
			}else{
				for($n=0;$n<((($p-3)/2)+1);$n++){
					$tplcontent2['level'].="<img src=\"$theme_imagespath/userlevel.gif\">&nbsp;";
				}
			}
		}
		
		$tplcontent2['posts'].=$userarray[6];
		
		$tplcontent['userinfo'].=template('postuserinfo',$tplcontent2);
	}else{
		$tplcontent2['name'].="Guest ($indpostarray[0])";
		
		$tplcontent['userinfo'].=template('postuserinfoguest',$tplcontent2);
	}
	
	$indpostarray[2]=htmlentities($indpostarray[2]);
	
	if(substr($topictime,0,10)=="important_"){
		$tplcontent['messagesubject'].=$languagedef[316].": ";
	}
	
	if($indpostarray[4]=="poll"){
		$tplcontent['messagesubject'].="$languagedef[299]: ";
	}
	
	$tplcontent['messagesubject'].="<a name=\"post".($post+1)."\">$indpostarray[2]</a>";
	
	//show timestamp
	$useroptarray=getdata($configarray[1]."/".$useridarray[$navboardlogin]."/options.php");
	if($useroptarray[4]=="on"){
		$tplcontent['messagesubject'].=' ('.$post.')';
	}
	
	########### START POLL STUFF #################
	if($indpostarray[4]=="poll"){
		$pollitems=explode("\t",$indpostarray[5]);
		$pollvotes=explode(",",$indpostarray[6]);
		if($indpostarray[7]){$pollvoters=explode(",",$indpostarray[7]);}
		
		if($showvoters==1){
			for($m=0;$m<count($pollvoters);$m++){
				$userarray2=getdata("$configarray[1]/$pollvoters[$m]/main.php");
				if($pollvoters[$m]){
					if(count($userarray2)>0){
						$tplcontent2['users'].="<a href=\"profile.php?user=$pollvoters[$m]\">$userarray2[0]</a>";
					}else{
						$tplcontent2['users'].="$languagedef[226] ($pollvoters[$m])";
					}
					if($m<(count($pollvoters)-1)){$tplcontent2['users'].=", ";}
				}
			}
			
			$tplcontent['messagebody'].=template('postrowwhovoted',$tplcontent2);
		}else{
			unset($tplcontent2);
			$tplcontent2['link'].="index.php?forum=$forum&topic=$topicnum&poststart=$poststart&showvoters=1";
			$tplcontent['messagebody'].=template("postrowwhovotedlink",$tplcontent2);
		}
		
		if($login==1&&!@in_array($useridarray[$navboardlogin],$pollvoters)){
			
			if(isset($vote)){
				unset($tplcontent2);
				$pollvotes[($vote-1)]+=1;
				$pollvoters[]=$useridarray[$navboardlogin];
				
				$pollvotesline=implode(",",$pollvotes);
				$pollvotersline=implode(",",$pollvoters);
				
				writedata("$configarray[2]/$forum/$topictime/0.php",$pollvotesline,6);
				writedata("$configarray[2]/$forum/$topictime/0.php",$pollvotersline,7);
				
				$tplcontent2['link'].="index.php?forum=$forum&topic=$topicnum&poststart=0";

				$tplcontent['messagebody'].=template('postrowvoteadded',$tplcontent2);
				
			}else{
				unset($tplcontent2);
				$tplcontent2['formheader'].="<form action=\"index.php\" method=get>";
				$tplcontent2['formheader'].="<input type=hidden value=\"$forum\" name=\"forum\">";
				$tplcontent2['formheader'].="<input type=hidden value=\"$topicnum\" name=\"topic\">";
				
				for($m=0;$m<count($pollitems);$m++){
					unset($tplcontent3);
					$tplcontent3['itemname'].=$pollitems[$m];

					$tplcontent3['itemselect'].="<input type=radio value=\"".($m+1)."\" name=\"vote\" class=\"forminput\">";
					
					$tplcontent2['polloptionsrows'].=template('postrowpolloptionsrow',$tplcontent3);
				}

				$tplcontent2['submitbutton'].="<input type=submit value=\"Vote\" class=\"forminput\">";
				$tplcontent2['formfooter'].="</form>";
				
				$tplcontent['messagebody'].=template('postrowpolloptions',$tplcontent2);
			}
			
		}else{
			unset($tplcontent2);
			$totalvotes=0;
			for($m=0;$m<count($pollitems);$m++){
				$totalvotes=$totalvotes+$pollvotes[$m];
			}
			
			for($m=0;$m<count($pollitems);$m++){
				unset($tplcontent3);
				$tplcontent3['itemname'].="$pollitems[$m]";
				
				$width = ($pollvotes[$m]/$totalvotes)*100;
				if($width<1){$width=1;}
				if($width>99){$width=99;}
				
				$tplcontent3['percentbar'].="<table width=\"$width%\" height=10 border=0 cellpadding=0 cellspacing=0>";
				$tplcontent3['percentbar'].="<tr>";
				$tplcontent3['percentbar'].="<td class=\"tablecell2=\">";
				$tplcontent3['percentbar'].="<img src=\"$theme_imagespath/poll_left.gif\" border=0>";
				
				$tplcontent3['percentbar'].="</td>";
				$tplcontent3['percentbar'].="<td height=10 width=\"100%\" background=\"$theme_imagespath/poll_mid.gif\">";
				
				$tplcontent3['percentbar'].="</td>";
				
				$tplcontent3['percentbar'].="<td>";
				$tplcontent3['percentbar'].="<img src=\"$theme_imagespath/poll_right.gif\" border=0>";
				$tplcontent3['percentbar'].="</td>";
				$tplcontent3['percentbar'].="</tr>";
				$tplcontent3['percentbar'].="</table>";
				
				
				if($totalvotes>0){
					$tplcontent3['stats'].=round(($pollvotes[$m]/$totalvotes)*100,1)."%";
				}else{
					$tplcontent3['stats'].="0%";
				}
				
				if($pollvotes[$m]>0){
					$tplcontent3['stats'].=" ($pollvotes[$m])";
				}else{
					$tplcontent3['stats'].=" (0)";
				}

				$tplcontent2['resultsrows'].=template('postrowpollresultsrow',$tplcontent3);
			}//item loop
			
			$tplcontent['messagebody'].=template('postrowpollresults',$tplcontent2);
		}
		
		$tplcontent['messagebody'].="<br>";
		
	}
	############# END POLL STUFF#################
	
	$body=bodyparse($indpostarray[3]);
	
	$tplcontent['messagebody'].=$body;

	if($indpostarray[4]=="attachment"&&file_exists("attachments/$indpostarray[5]")){
		unset($tplcontent2);
		$tplcontent2['name'].=$indpostarray[5];
		$tplcontent2['link'].="attachments/$indpostarray[5]";
		$tplcontent['messagebody'].=template('postrowattachment',$tplcontent2);
	}

	if($indpostarray[11]){
		$editarray=explode(",",$indpostarray[11]);
		if($configarray[46]!=="on"){
			$lastedit=count($editarray)-2;
			$userarray2=getdata("$configarray[1]/$editarray[$lastedit]/main.php");
			unset($tplcontent2);
			$tplcontent2['user'].="<a href=\"profile.php?user=$editarray[$lastedit]\">$userarray2[0]</a>";
			$tplcontent2['date'].=date($dateformat,$editarray[$lastedit+1]+$timeoffsetseconds);
			$tplcontent['messagebody'].=template('postrowlasteditby',$tplcontent2);
		}else{
			for($n=0;$n<count($editarray);$n+=2){
				$userarray2=getdata("$configarray[1]/$editarray[$n]/main.php");
				unset($tplcontent2);
				if(count($userarray2)>0){
					$tplcontent2['user'].=$userarray2[0];
				}else{
					$tplcontent2['user'].="Guest (".$editarray[$n].")";
				}
				$tplcontent2['date'].=date($dateformat,$editarray[$n+1]+$timeoffsetseconds);
				$tplcontent['messagebody'].=template('postroweditedby',$tplcontent2);
			}
		}//only show last edit check
	}
	$tplcontent['messagebody'].="</span>";
	
	if($userarray[3]){
		$userarray[3]=BBCode($userarray[3],"1");
		$tplcontent['signature'].=$userarray[3];
	}
	
	$date=date($dateformat,$indpostarray[1]+$timeoffsetseconds);
	$tplcontent['date'].=$date;
	
	$postnumber=$post+1;
	
	if(geteditaccess()&&getforumaccess("edit")&&!($topicconfigarray[0]=="closed"&&$userloggedinarray[15]!=="administrator")){
		$tplcontent['buttons'].="<a href=\"post.php?forum=$forum&topic=$topicnum&post=$postnumber&mode=edit&page=$page\"><img src=\"$theme_imagespath/edit.gif\" border=0></a>";
	}
	
	if(getforumaccess("reply")){
		$tplcontent['buttons'].="<a href=\"post.php?forum=$forum&topic=$topicnum&post=$postnumber&mode=quote\"><img src=\"$theme_imagespath/quote.gif\" border=0></a>";
	}
	
	$userarray=getdata("$configarray[1]/$indpostarray[0]/main.php");
	
	if(count($userarray)>0){
		$tplcontent['buttons'].="<a href=\"profile.php?user=$indpostarray[0]\"><img src=\"$theme_imagespath/profile.gif\" border=0></a>";
	}
	
	if($userarray[9]) {
		$tplcontent['buttons'].="<a href=\"$userarray[9]\" target=\"_new\"><img src=\"$theme_imagespath/website.gif\" border=0></a>";
	}
	
	if(count($userarray)>0&&$login==1){
		$tplcontent['buttons'].="<a href=\"user_pm.php?selected=$indpostarray[0]&mode=send\"><img src=\"$theme_imagespath/pm2.gif\" border=0></a>";
	}
	
	if($userloggedinarray[15]=="administrator"){
		$tplcontent['ip'].=$indpostarray[9];
 		$tplcontent['banlink'].="admin_users.php?ban=$indpostarray[9]";
	}
	
	$tplcontent['deletebutton'].="<a href=\"post.php?forum=$forum&topic=$topicnum&post=$post&mode=deletepost&step=2&page=$page\"><img src=\"$theme_imagespath/nonew.gif\" alt=\"Delete This Post\" border=0></a>";
	
	$content.=template('postrow',$tplcontent);
	
	return $content;
}

function sendpm($sendto,$user,$subject,$body){
	global $configarray;
	
	$body=ereg_replace("\n","",$body);
	$body=ereg_replace("\r","[br]",$body);
	$body=stripslashes($body);
	
	$subject=ereg_replace("\n","",$subject);
	$subject=ereg_replace("\r","",$subject);
	$subject=stripslashes($subject);
	
	$total=checkpmspace($sendto);
	
	if($total['percentsize']>=100||$total['percentnumber']>=100){
		$content.="The selected user does not have enough space to store your pm, sorry";
	}else{
		
		if(trim($subject)&&trim($body)&&$sendto){
			$userpms=listfiles("$configarray[1]/$sendto/pms");
			@rsort($userpms,SORT_NUMERIC);
			$end=$userpms[0]+1;
			
			createdir("$configarray[1]/$sendto/pms");
			
			writedata("$configarray[1]/$sendto/pms/$end.php",$user,0);
			writedata("$configarray[1]/$sendto/pms/$end.php",$globaltime,1);
			writedata("$configarray[1]/$sendto/pms/$end.php",$subject,2);
			writedata("$configarray[1]/$sendto/pms/$end.php",$body,3);
			writedata("$configarray[1]/$sendto/pms/$end.php","unread",8);
			
			$content.="Private message sent";
		}else{
			$content.="Message must have send to, subject, body all filled out";
		}//fields check
		
	}//space check
	
	return $content;
}

function bbcodearray(){
	global $globalbbcodearray,$configarray;
	
	if(isset($globalbbcodearray)){
		$bbcodearray=$globalbbcodearray;
	}else{
		$globalbbcodearray=$bbcodearray=getdata("$configarray[5]/bbcode.php");
		if(count($globalbbcodearray)<1){$globalbbcodearray=array();}
		$bbcodearray=$globalbbcodearray;
	}
	
	return $bbcodearray;
}

function smiliesarray(){
	global $globalsmiliesarray,$configarray;
	
	if(isset($globalsmiliesarray)){
		$smiliesarray=$globalsmiliesarray;
	}else{
		$globalsmiliesarray=$smiliesarray=getdata("$configarray[5]/smilies.php");
		if(count($globalsmiliesarray)<1){$globalsmiliesarray=array();}
		$smiliesarray=$globalsmiliesarray;
	}
	
	return $smiliesarray;
}

/**
* @desc Prints a template with content into the page
*/
function template($templatename,$content=""){
	global $theme,$alltemplates,$replacements,$theme_templatespath,$theme_replacementspath,$theme_imagespath,$tplused,$tplusedcache,$languagedef;
	
	if($alltemplates[$templatename]){
		$template=$alltemplates[$templatename];
		$tplusedcache++;
	}else{
		if(!$dataarray=@file("$theme_templatespath/$templatename.php")){
			echo "<font color=\"red\">Error: Cannot find template '$theme_templatespath/$templatename.php'</font>";
		}else{
			@array_shift($dataarray);
			$template=implode("",$dataarray);
		
			$alltemplates[$templatename]=$template;
			$tplused++;
		}
	}
	
	if(!count($replacements)>0){
		if(!include("$theme_replacementspath/replacements.php")){
			echo "<font color=\"red\">Error: Cannot find replacements '$theme_replacementspath/replacements.php'</font>";
		}
	}
	$template=strtr($template,$replacements);
	
	//content
	$pattern="/.?<content:([a-zA-Z0-9]*)>.?/i";
	preg_match_all($pattern,$template,$contentlist);
	
	//only peform if these exist
	if(count($contentlist[1])>0){
		$contentlist=$contentlist[1];
		
		$trans=array();
		
		for($n=0;$n<count($contentlist);$n++){
			$trans["<content:".$contentlist[$n].">"]=$content[$contentlist[$n]];
		}
		$template=strtr($template,$trans);
	}
	
	//templates inside of the template
	$pattern="/.?<template:(.+)>.?/i";
	preg_match_all($pattern,$template,$templatelist);
	
	//only peform if these exist
	if(count($templatelist[1])>0){
		$templatelist=$templatelist[1];
		$trans=array();
		
		for($n=0;$n<count($templatelist);$n++){
			$trans["<template:".$templatelist[$n].">"]=template($templatelist[$n]);
		}
		$template=strtr($template,$trans);
	}
	
	$pattern="/.?<lang:([0-9]*)>.?/i";
	preg_match_all($pattern,$template,$langlist);
	
	//only peform if these exist
	if(count($langlist[1])>0){
		$langlist=$langlist[1];
		$trans=array();
		
		for($n=0;$n<count($langlist);$n++){
			$trans["<lang:".$langlist[$n].">"]=$languagedef[$langlist[$n]];
		}
		$template=strtr($template,$trans);
	}
	
	$modulesarray=listdirs("modules");
	
	for($m=0;$m<count($modulesarray);$m++){
		$pattern="/.?<lang:".$modulesarray[$m].":([0-9]*)>.?/i";

		preg_match_all($pattern,$template,$langlist);
	
			//only peform if these exist
			if(count($langlist[1])>0){
				$langlist=$langlist[1];
				$trans=array();
		
			for($n=0;$n<count($langlist);$n++){
				$t="<lang:".$modulesarray[$m].":".$langlist[$n].">";

				$trans[$t]=$languagedef[$modulesarray[$m]][$langlist[$n]];
			}
			
			$template=strtr($template,$trans);
		}
	}	
	
	//$template=str_replace("\'","'",$template);
	return $template;
}

/**
* @desc Creates a bar percentage filled
*/
function percentbar($percent,$width){
	global $theme_imagespath;
	
	$content.="<table width=\"$width\" height=\"10\" cellspacing=0 cellpadding=0 class=\"table\">
	<tr><td class=\"tablecell1\">";
	if($percent<1){$percent=1;}
	if($percent>99){$percent=99;}
	$content.="<table height=10 cellspacing=0 cellpadding=0 border=0 width=\"$percent%\">
	<tr>
	<td background=\"$theme_imagespath/poll_mid.gif\">
	</td>
	</tr>
	</table>
	</td>
	</tr>
	</table>";
	
	return $content;
}

function forumsfootermenu($parentforum,$level){
	global $configarray,$forumlist,$allforumarray,$footer;
	if($forumlist[$parentforum]){$forumsublist=explode(",",$forumlist[$parentforum]);}
	for($m=0;$m<count($forumsublist);$m++){
		$forumconfigarray=getdata("$configarray[2]/$forumsublist[$m].php");
		if(getforumaccess("show",$forumsublist[$m])&&$level<$configarray[27]){
			$footer['navigation'].="<option value=\"index.php?forum=$forumsublist[$m]\">";
			
			for($l=0;$l<$level;$l++){
				$footer['navigation'].=">";
			}
			
			$footer['navigation'].=" $forumconfigarray[3]</option>";
			forumsfootermenu($forumsublist[$m],$level+1,$selected);
		}//access/level test
	}//loop
}


?>
